const express = require("express")
const adminRouter = express.Router()

const adminSevr = require("../../services/adminServices")



adminRouter.post("/register", async(req,res,next)=>{
    try {
        const result = await adminSevr.addAdmin(req.body)
        if(result){
            res.send(result)
        }
    } catch (error) {
        next(error)
    }
})


adminRouter.post("/login", async(req,res,next)=>{
    try {
        const result = await adminSevr.login(req.body)
        if(result){
            res.cookie("token",result.id,{
                path : "/",
                domain : "localhost",
                maxAge : 7 * 24 * 3600 * 1000,
                signed : true
            })
            res.header("authorization",result.id);
        }
        res.send(result)
    } catch (error) {
        next(error)
    }
})

adminRouter.get("/tx", async(req,res,next)=>{
    try {
        const result = await adminSevr.getTx(req.query.id)
        if(result){
            res.send(result)
        }
    } catch (error) {
        next(error)
    }
})

adminRouter.get("/getYhm", async(req,res,next)=>{
    try {
        const result = await adminSevr.getYhm(req.query.userID)
        if(result){
            res.send(result)
        }
    } catch (error) {
        next(error)
    }
})


module.exports = adminRouter